package view.room;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import constants.Constants;
import entity.RoomType;
import controllers.*;

public class RoomPanel extends JPanel{
	String[] typeNames;
	String[] statusNames = Constants.STATUS_NAME;
	ArrayList<RoomType> roomTypes ;
	
	int typeFilter   = Constants.ALL_TYPE;
	int statusFilter = Constants.ALL_STATUS;
	
	RoomContent roomContent;
	SideButton sideButton;
	
	public RoomPanel(ArrayList<RoomType> roomTypes){
                this.roomTypes=roomTypes;
		setLayout(new BorderLayout());		
		getData();
		add(roomContent= new RoomContent());
		add(sideButton = new SideButton(), BorderLayout.EAST);
	}
	
	public void getData(){
		typeNames = new String[roomTypes.size()];
		for (int i=0; i<roomTypes.size(); i++){
			typeNames[i] = roomTypes.get(i).getName();
		}
	}
	public class SideButton extends JPanel{
		
		public SideButton(){
			setLayout(new FlowLayout(FlowLayout.LEFT));
			setPreferredSize(new Dimension(130, 20));
			setBorder(BorderFactory.createLineBorder(Color.GRAY));
			
			refresh();
		}
		public void refresh(){
			removeAll();
			
			JLabel lbType = new JLabel("LOẠI PHÒNG");
			lbType.setPreferredSize(new Dimension(120, 30));
			add(lbType);
			
			for (int i=0; i<roomTypes.size(); i++){
				JButton btType = new JButton(roomTypes.get(i).getName());
				btType.setPreferredSize(new Dimension(120,30));
				btType.addActionListener(new TypeFilterListener(i));
				add(btType);
			}
			
			JLabel lbStatus = new JLabel("TÌNH TRẠNG");
			lbStatus.setPreferredSize(new Dimension(120, 30));
			add(lbStatus);
			
			for (int i=0; i<statusNames.length; i++){
				JButton btStatus = new JButton(statusNames[i]);
				btStatus.setPreferredSize(new Dimension(120,30));
				btStatus.addActionListener(new StatusFilterListener(i));
				add(btStatus);
			}
		}
	}
	
	public void setNormal(){
		
	}
	
	public void refreshAll(){
		roomContent.refreshAll();
		sideButton.refresh();
	}
	class TypeFilterListener implements ActionListener{
		private int index;
		public TypeFilterListener(int index){
			this.index = index;
		}
		public void actionPerformed(ActionEvent event){
			typeFilter = index;
			roomContent.filter(typeFilter, statusFilter);
		}
	}
	
	class StatusFilterListener implements ActionListener{
		private int index;
		public StatusFilterListener(int index){
			this.index = index;
		}
		public void actionPerformed(ActionEvent event){
			statusFilter = index;
			roomContent.filter(typeFilter, statusFilter);
		}
	}
}
